package yagwl.client.widget;

import java.util.List;
import java.util.logging.Logger;

import yagwl.client.model.FilterDTO;
import yagwl.client.model.FilteredItemListCache;
import yagwl.client.model.ItemDTO;
import yagwl.client.widget.internal.Filter;
import yagwl.client.widget.internal.Filter.FilterSelectionChangeHandler;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.SimplePanel;

public class FilteredTable extends Composite {

  private static final Logger log = Logger.getLogger(FilteredTable.class
      .getName());
  private final SimplePanel rootSP = new SimplePanel();
  private final Filter filter = new Filter(new FilterChangeHandler());
  private final PagedCellTable pagedTable = new PagedCellTable();
  private FilteredItemListCache filteredListCache;
  private FilterLocation location;

  public FilteredTable() {
    rootSP.setWidth("100%");
    rootSP.addStyleName("ya-filterTable");
    setFilterLocation(FilterLocation.Top);
    initWidget(rootSP);
  }

  public void setFilteredListCache(FilteredItemListCache filteredListCache) {
    this.filteredListCache = filteredListCache;
    pagedTable.setListCache(filteredListCache);
    filter.setFilteredListCache(filteredListCache);
  }

  public void renderUI() {
    filter.renderUI();

    Grid grid;
    if (location == FilterLocation.Top) {
      grid = new Grid(2, 1);
      grid.setWidget(0, 0, filter);
      grid.setWidget(1, 0, pagedTable);
      grid.getCellFormatter().addStyleName(0, 0, "ya-filterTable-filter-row");
      grid.getCellFormatter().addStyleName(1, 0, "ya-filterTable-data-row");
    } else {
      grid = new Grid(1, 2);
      grid.setWidget(0, 0, filter);
      grid.setWidget(0, 1, pagedTable);
      grid.getCellFormatter()
          .addStyleName(0, 0, "ya-filterTable-filter-column");
      grid.getCellFormatter().addStyleName(0, 1, "ya-filterTable-data-column");
      grid.getCellFormatter().setVerticalAlignment(0, 0,
          HasVerticalAlignment.ALIGN_TOP);
      grid.getCellFormatter().setVerticalAlignment(0, 1,
          HasVerticalAlignment.ALIGN_TOP);
    }
    rootSP.setWidget(grid);
    grid.setWidth("100%");
  }

  private class FilterChangeHandler implements FilterSelectionChangeHandler {
    @Override
    public void onChange(List<FilterDTO> selectedFilters) {
      for (FilterDTO filter : selectedFilters) {
        switch (filter.getFilterType()) {
        case MultiSelect:
        case SingleSelect:
          StringBuffer buf = new StringBuffer(filter.getId() + ": ");
          for (ItemDTO item : filter.getSelectList()) {
            buf.append(item.getLabel() + " ");
          }
          log.info(buf.toString());
          break;
        case IntegerRange:
          log.info(filter.getId() + ": " + filter.getMinValue() + " "
              + filter.getMaxValue());
        }
      }

      filteredListCache.setSelectedFilters(selectedFilters);
      pagedTable.getCellTable().setPageStart(0);
      filteredListCache.fetchNext();
      // in future, also call filteredListCache.fetchFilters
    }
  }

  public PagedCellTable getPagedCellTable() {
    return pagedTable;
  }

  public void setFilterLocation(FilterLocation location) {
    this.location = location;
    filter
        .setDirection(location == FilterLocation.Top ? Filter.Direction.Horizontal
            : Filter.Direction.Vertical);
  }

  public void setMinRowsAndHeightForScroll(int minRows, String height) {
    filter.setMinRowsAndHeightForScroll(minRows, height);
  }

  public enum FilterLocation {
    Top, Left
  }
}
